package com.me.mygdxgame;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.World;



public class GameWorld {
	
Mario mario;
ArrayList<Box>boxes;

World world;
GameWorldRender render;
public static final float PIXELS_PER_METER = 60f;
public float jumpForce;

boolean jumperFacingRight=false;
Rectangle left;
Rectangle right;
Rectangle jump;
Vector3 touchPoint;
OrthographicCamera guicam;
SpriteBatch spriteBatch;


public GameWorld(World world){
	this.world=world;
	boxes=new ArrayList<Box>();
	render=new GameWorldRender(this);
	mario=new Mario(world,1,2);
	
	guicam=new OrthographicCamera(render.getScreenWidth(),render.getScreenHeight());
	guicam.position.set(render.getScreenWidth() / 2,render.getScreenHeight() / 2, 0);
	guicam.update();

	spriteBatch=new SpriteBatch();
	
	
	left=new Rectangle( render.getScreenWidth()/10, 10, 64, 64);
	right=new Rectangle(render.getScreenWidth()/10*3, 10, 64, 64);
	jump=new Rectangle(render.getScreenWidth()-render.getScreenWidth()/10*2,10,64,64);
	touchPoint = new Vector3();

	jumpForce=5500.0f;
	if(Gdx.graphics.getHeight()<=320 && Gdx.graphics.getWidth()<=480){
		jumpForce=4000.0f;
	}
	
}
	

public void SetMario(Mario mario){
	this.mario=mario;
}


public void Render(){
	render.Render();
}


public void Update(float delta){
	
	int estado=mario.State_Stop;


	if (Gdx.input.isTouched(0)) {
		
		guicam.unproject(touchPoint.set(Gdx.input.getX(0), Gdx.input.getY(0), 0));
		
		Rectangle t=new Rectangle(touchPoint.x,touchPoint.y,32,32);
		
		if(right.overlaps(t)){
			mario.jumper.applyForce(new Vector2(250.0f, 0.0f), new Vector2(
					mario.W / (2 * PIXELS_PER_METER),
					mario.H / (2 * PIXELS_PER_METER)));	
			
			estado=mario.State_Move_RIGHT;

		}
		else{
			if(left.overlaps(t)){
				mario.jumper.applyForce(new Vector2(-250.0f, 0.0f), new Vector2(
						mario.W / (2 * PIXELS_PER_METER),
						mario.H / (2 * PIXELS_PER_METER)));	
				estado=mario.State_Move_LEFT;

			}
			else{
				
				if(jump.overlaps(t) && Math.abs(mario.jumper.getLinearVelocity().y) < 1e-9){
					mario.jumper.applyForce(new Vector2(0.0f, jumpForce), new Vector2(
							mario.W / (2 * PIXELS_PER_METER),
							mario.H / (2 * PIXELS_PER_METER)));
					estado=mario.State_Jump;
					
				}
				
			}
		}
		
	}


	if(Gdx.input.isTouched(1)){
	
		guicam.unproject(touchPoint.set(Gdx.input.getX(1), Gdx.input.getY(1), 0));
		
		Rectangle r=new Rectangle(touchPoint.x,touchPoint.y,32,32);
		
		if(right.overlaps(r)){
			mario.jumper.applyForce(new Vector2(250.0f, 0.0f), new Vector2(
					mario.W / (2 * PIXELS_PER_METER),
					mario.H / (2 * PIXELS_PER_METER)));	
		}
		else{
			if(left.overlaps(r)){
				mario.jumper.applyForce(new Vector2(-250.0f, 0.0f), new Vector2(
						mario.W / (2 * PIXELS_PER_METER),
						mario.H / (2 * PIXELS_PER_METER)));	
			}
			else{
				
				if(jump.overlaps(r) && Math.abs(mario.jumper.getLinearVelocity().y) < 1e-9){
					mario.jumper.applyForce(new Vector2(0.0f, jumpForce), new Vector2(
							mario.W / (2 * PIXELS_PER_METER),
							mario.H / (2 * PIXELS_PER_METER)));
				}
				
			}
		}
	}


	
	
	
	/*
	
	
	if (Gdx.input.isKeyPressed(Input.Keys.VOLUME_UP)) {
		mario.jumper.applyForce(new Vector2(450.0f, 0.0f), new Vector2(
				mario.W / (2 * PIXELS_PER_METER),
				mario.H / (2 * PIXELS_PER_METER)));
		
	
	} else if (Gdx.input.isKeyPressed(Input.Keys.VOLUME_DOWN)) {
		mario.jumper.applyForce(new Vector2(-450.0f, 0.0f), new Vector2(
				mario.W / (2 * PIXELS_PER_METER),
				mario.H / (2 * PIXELS_PER_METER)));
	}

	/**
	 * The jumper dude can only jump while on the ground. There are better
	 * ways to detect ground contact, but for our purposes it is sufficient
	 * to test that the vertical velocity is zero (or close to it). As in
	 * the above code, the 4500N figure here was found through
	 * experimentation. It's enough to get the guy off the ground.
	 * 
	 * As before, force is applied to the center of the jumper.
	 */
	
	/*
	if (Gdx.input.justTouched()
			&& Math.abs(mario.jumper.getLinearVelocity().y) < 1e-9) {
		mario.jumper.applyForce(new Vector2(0.0f, 4500.0f), new Vector2(
				mario.W / (2 * PIXELS_PER_METER),
				mario.H / (2 * PIXELS_PER_METER)));
	}
	*/

	/**
	 * Have box2d update the positions and velocities (and etc) of all
	 * tracked objects. The second and third argument specify the number of
	 * iterations of velocity and position tests to perform -- higher is
	 * more accurate but is also slower.
	 */
	
	mario.Update(delta,estado);
	
	
	world.step(Gdx.app.getGraphics().getDeltaTime(), 3, 3);
	world.clearForces();
	
	Gdx.app.log("JumperTutorial", "Delta time: " + Gdx.app.getGraphics().getDeltaTime());

	Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

	
}



}
